package com.yanqu.road.server.gameplayer.module.assistant;

import com.yanqu.road.dao.impl.player.AssistantSettingUserDataDaoImpl;
import com.yanqu.road.dao.impl.player.UserAssistantZsSetDaoImpl;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.assistant.AssistantSettingUserData;
import com.yanqu.road.entity.assistant.AssistantShopSet;
import com.yanqu.road.entity.assistant.UserAssistantZsSetData;
import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.BuyGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.zhaoshang.ZsProjectInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.estatewar.palace.UserPalaceData;
import com.yanqu.road.entity.player.ReturnData;
import com.yanqu.road.logic.bussiness.player.UserAssistantShopBusiness;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.mall.MallModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.gameplayer.module.zhaoshang.ZsModule;
import com.yanqu.road.server.logic.bag.BaseUseGoods;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.MallActivityMgr;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.palace.PalaceActivity;
import com.yanqu.road.server.manger.palace.PalaceActivityMgr;
import com.yanqu.road.server.manger.zhaoshang.ZsConfigMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/9/14 16:55
 */
public class AssistantModule extends GeneralModule {
    private static Logger logger = LogManager.getLogger(AssistantModule.class.getName());
    /**
     * 小助理商店设置 key:商品id
     */
    private Map<Integer, AssistantShopSet> assistantShopSetMap = new ConcurrentHashMap<>();
    private MallSort mallSort = new MallSort();
    private UserAssistantZsSetData userAssistantZsSetData = null;
    AssistantProto.AssistantActionRespMsg.Builder actionBuilder = null;

    // 小玉设置信息
    private AssistantSettingUserData settingUserData = null;

    public AssistantModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        assistantShopSetMap = UserAssistantShopBusiness.getAssistantShopMap(player.getUserId());
        userAssistantZsSetData = new UserAssistantZsSetDaoImpl().getUserAssistantZsSetData(player.getUserId());
        settingUserData = new AssistantSettingUserDataDaoImpl().getAssistantSettingUserData(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public void afterLogin() {
        checkPalaceMall();              //皇城先看下有没有
//        syncAssistantShopSet();
//        syncZsSet();
        // 同步小玉助手设置
//        syncAssistantSetting();
    }

    public void syncAssistantSetting() {
        if (settingUserData == null) {
            settingUserData = new AssistantSettingUserData();
            settingUserData.setUserId(player.getUserId());
            settingUserData.setOperateTypeList(new ArrayList<>());
            settingUserData.setBeautyId(0);
            settingUserData.setBeautyFavorHundred(false);
            settingUserData.setOpenComprehend(false);
            settingUserData.setGrandChildEnterSchoolMap(new ConcurrentHashMap<>());
            settingUserData.setCharitableTimes(0);
            settingUserData.setInsertOption();
        }
        // 同步数据
        AssistantProto.AssistantSettingSyncMsg.Builder syncMsg = AssistantProto.AssistantSettingSyncMsg.newBuilder();
        syncMsg.addAllOperateEnum(settingUserData.getOperateTypeList());
        syncMsg.setCharitableTimes(settingUserData.getCharitableTimes());
        syncMsg.setId(settingUserData.getBeautyId());
        for (Map.Entry<Integer, Integer> entry : settingUserData.getGrandChildEnterSchoolMap().entrySet()) {
            AssistantProto.AssistantGrandChildEnterSchoolItem.Builder enterSchoolTemp = AssistantProto.AssistantGrandChildEnterSchoolItem.newBuilder();
            enterSchoolTemp.setSchoolType(entry.getKey());
            enterSchoolTemp.setPatronsId(entry.getValue());
            syncMsg.addGrandChildEnterSchool(enterSchoolTemp);
        }
        syncMsg.setOpenComprehend(settingUserData.isOpenComprehend());
        syncMsg.setBeautyFavorHundred(settingUserData.isBeautyFavorHundred());
        player.sendPacket(ClientProtocol.U_ASSISTANT_SETTING_SYNC, syncMsg);
    }

    public AssistantSettingUserData getSettingUserData() {
        return settingUserData;
    }

    @Override
    public void loginSendMsg() {
        syncAssistantShopSet();
        syncZsSet();
        syncAssistantSetting();
    }

    public void syncAssistantShopSet() {
        checkUnionMall();
        player.sendPacket(ClientProtocol.U_ASSISTANT_SHOP_SET_SYNC, parseSyncMsg(new ArrayList<>(assistantShopSetMap.values())));
    }

    private void checkUnionMall() {
        for (AssistantShopSet assistantShopSet : assistantShopSetMap.values()) {
            int mallId = assistantShopSet.getMallId();
            MallInfo mallInfo = MallMgr.getMallInfoByMallId(mallId);
            if (null == mallInfo) {
                continue;
            }
            if (MallMgr.isUnionLimit(player, mallInfo)) {
                assistantShopSet.setCount(0L);
            }
        }
    }

    public boolean checkPalaceMall() {
        boolean hasPalaceLimit = false;
        int palacePos = 0;
        int titleType = 0;
        PalaceActivity palaceActivity = PalaceActivityMgr.getInTimePalaceActivity();
        if(null != palaceActivity){
            UserPalaceData userPalaceData = palaceActivity.getUserPalaceData(player.getUserId());
            if(null != userPalaceData){
                palacePos = userPalaceData.getPos();
            }
            titleType = palaceActivity.getTitleType(palacePos);
        }
        for (AssistantShopSet assistantShopSet : assistantShopSetMap.values()) {
            int mallId = assistantShopSet.getMallId();
            MallInfo mallInfo = MallMgr.getMallInfoByMallId(mallId);
            if (null == mallInfo) {
                continue;
            }
            if (MallMgr.isPalaceMallLimit(titleType, mallInfo)) {
                //皇城商店
                if (mallInfo.getShopType() == eShopType.PalaceExchangeMall.getValue()) {
                    assistantShopSet.setCount(0);
                }
                hasPalaceLimit = true;
                break;
            }
        }
        return hasPalaceLimit;
    }

    /**
     * 商店购买数量设置
     * @param mallId    商品id
     * @param count     数量
     * @return ret
     */
    public int setAssistantGoodsPreBuy(int mallId, long count) {
        //商品是否存在
        MallInfo mallInfo = MallMgr.getMallInfoByMallId(mallId);
        if (null == mallInfo) {
            return GameErrorCode.E_MALL_NO_EXIST;
        }
        //商店类型是否正确
        if (!assistantShopTypeOrderList().contains(mallInfo.getShopType())) {
            return GameErrorCode.E_MALL_BUY_SHOP_TYPE_ERROR;
        }
        //VIP限制
        int vipLevel = VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp());
        if (count > 0 && vipLevel < mallInfo.getVipLimit()) {
            return GameErrorCode.E_MALL_LIMIT_BUY_VIP;
        }
        //系统解锁限制
        if (mallInfo.getSystemLimit().size() > 0) {
            for (Integer systemId : mallInfo.getSystemLimit()) {
                if (!SystemOpenMgr.systemOpen(player, systemId)) {
                    return GameErrorCode.E_SYSTEM_NO_OPEN;
                }
            }
        }
        if (mallInfo.getCategory() != eMallCategory.NormalProp.getValue()) {
            return GameErrorCode.E_MALL_NO_EXIST;
        }
        //商会限购
        if (MallMgr.isUnionLimit(player, mallInfo)) {
            return GameErrorCode.E_MALL_BUY_UNION_LEVEL_NO_ENOUGH;
        }
        //预设值不得大于限购
        long limitBuyCount = player.getModule(MallModule.class).getMallInfoLimitBuyCount(mallId);
        if (count > limitBuyCount) {
            return GameErrorCode.E_MALL_LIMIT_BUY;
        }
        //设置值
        putGoodsSetValue(mallId, count);
        //同步
        syncAssistantShopSet();
        return 0;
    }

    private void putGoodsSetValue(int mallId, long count) {
        if (!assistantShopSetMap.containsKey(mallId)) {
            AssistantShopSet assistantShopSet = new AssistantShopSet(player.getUserId(), mallId, count, DataStatus.Insert);
            assistantShopSetMap.put(assistantShopSet.getMallId(), assistantShopSet);
        } else {
            AssistantShopSet assistantShopSet = assistantShopSetMap.get(mallId);
            assistantShopSet.setCount(count);
        }
    }

    /**
     * 小助理购买
     * @return
     */
    public AssistantProto.AssistantShopBuyRespMsg.Builder assistantBuy() {
        synchronized (assistantShopSetMap) {
            AssistantProto.AssistantShopBuyRespMsg.Builder respMsg = AssistantProto.AssistantShopBuyRespMsg.newBuilder();
            respMsg.setRet(0);
            //根据商品商店类型分类
            Map<Integer, List<AssistantShopSet>> shopTypeMap = transferShopTypeMap();
            //按商店排序 默认排序 进行购买
            for (Integer shopType : assistantShopTypeOrderList()) {
                AssistantProto.AssistantShopBuyResult.Builder result = AssistantProto.AssistantShopBuyResult.newBuilder();
                result.setShopType(shopType);
                result.setRet(0);
                List<AssistantShopSet> assistantShopSetList = shopTypeMap.get(shopType);
                if (assistantShopSetList != null) {
                    //数量都是0
                    if (assistantShopSetList.stream().noneMatch(assistantShopSet -> assistantShopSet.getCount() > 0)) {
                        result.setRet(GameErrorCode.E_ASSISTANT_SHOP_NO_SET);
                        respMsg.addRessult(result);
                        continue;
                    }
                    //商会商店 玩家没商会 返回错误码
                    if (shopType == eShopType.UnionMall.getValue() && StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {
                        result.setRet(GameErrorCode.E_UNION_NO_JOIN_UNION);
                        respMsg.addRessult(result);
                        continue;
                    }

                    Property reward = new Property();   //购买所得
                    Property activityConsumeProperty = new Property();  //活动购买消耗
                    Property consumeProperty = new Property();  //购买消耗
                    //商品也要排序 默认排序tmd 写死了 (又不要了)
//                    assistantShopSetList.sort(mallSort);
                    boolean finishBuy = true; //是否都买过了
                    String buyDesc = "";  //日志记录

                    for (AssistantShopSet assistantShopSet : assistantShopSetList) {
                        //已买数量
                        int mallBuyCount = player.getModule(MallModule.class).getMallBuyCount(assistantShopSet.getMallId());

                        if(shopType == eShopType.WaSheExchangeActivityMall.getValue()){
                            //瓦舍兑换商店
                            mallBuyCount = player.getModule(ActivityMallModule.class).getMallBuyCount(assistantShopSet.getMallId());
                        }

                        if (assistantShopSet.getCount() > mallBuyCount) {
                            finishBuy = false;
                            int buyCount = (int) (assistantShopSet.getCount() - mallBuyCount);
                            //预购买 走原来的购买逻辑
                            ReturnData returnData = player.getModule(MallModule.class).preBuyGoods(assistantShopSet.getMallId(), buyCount);
                            //买不了 设置错误码
                            if (returnData.getRet() != 0) {
                                result.setRet(returnData.getRet());
                                break;
                            } else {
                                //可以买 累加消耗
                                if(shopType == eShopType.WaSheExchangeActivityMall.getValue()){
                                    activityConsumeProperty.addProperty(PropertyHelper.parseStringToProperty(returnData.getParams().get(0)));
                                }else {
                                    consumeProperty.addProperty(PropertyHelper.parseStringToProperty(returnData.getParams().get(0)));
                                }
                                buyDesc += assistantShopSet.getMallId() + ":" + buyCount + ";";
                            }
                        }
                    }

                    log.info("assistantBuy userId:{},buy:{}", player.getUserId(), buyDesc);
                    buyDesc = "";
                    if (finishBuy) { //都买完了 再给他一个错误码
                        result.setRet(GameErrorCode.E_ASSISTANT_SHOP_FINISH_BUY);
                        respMsg.addRessult(result);
                        continue;
                    }

                    if (result.getRet() == 0){ //都可以买
                        if (player.getModule(CurrencyModule.class).removeCurrency(consumeProperty, eLogMoneyType.Mall, eLogMoneyType.MallBuy, buyDesc)) {
                            for (AssistantShopSet assistantShopSet : assistantShopSetList) {
                                if(assistantShopSet.getCount() <= 0){
                                    continue;
                                }
                                if(shopType == eShopType.WaSheExchangeActivityMall.getValue()){
                                    handleWaSheBuy(reward, assistantShopSet);
                                    continue;
                                }else {
                                    int mallBuyCount = player.getModule(MallModule.class).getMallBuyCount(assistantShopSet.getMallId());
                                    if(assistantShopSet.getCount() - mallBuyCount <= 0){
                                        continue;
                                    }
                                    String rewardStr = player.getModule(MallModule.class).buyGoods(assistantShopSet.getMallId(), (int) (assistantShopSet.getCount() - mallBuyCount));
                                    reward.addProperty(PropertyHelper.parseStringToProperty(rewardStr));
                                }
                            }
                        } else {
                            result.setRet(GameErrorCode.E_BAG_ITEM_NO_ENOUGH);
                        }
                        result.setBuyReward(PropertyHelper.parsePropertyToString(reward));
                        consumeProperty.addProperty(activityConsumeProperty);
                        result.setBuyCost(PropertyHelper.parsePropertyToString(consumeProperty));
                        respMsg.addRessult(result);
                    } else {
                        respMsg.addRessult(result);
                    }

                } else {    //还没设置这个类型的商店 再给一个错误码
                    result.setRet(GameErrorCode.E_ASSISTANT_SHOP_NO_SET);
                    respMsg.addRessult(result);
                }
            }
            return respMsg;
        }
    }

    private void handleWaSheBuy(Property reward, AssistantShopSet assistantShopSet) {
        try {
            //瓦舍兑换商店
            MallInfo mallInfo = MallActivityMgr.getMallInfo(assistantShopSet.getMallId());
            if (mallInfo == null) {
                return;
            }
            int mallBuyCount = player.getModule(ActivityMallModule.class).getMallBuyCount(assistantShopSet.getMallId());
            if (assistantShopSet.getCount() - mallBuyCount <= 0) {
                return;
            }
            BuyGoodsResult buyGoodsResult = player.getModule(ActivityMallModule.class).buyGoods(mallInfo, (int) (assistantShopSet.getCount() - mallBuyCount));
            if (buyGoodsResult.getRet() == 0) {//已经加背包了，直接使用道具
                //盘面技能宝箱
                Property property = PropertyHelper.parseStringToProperty(buyGoodsResult.getReward());
                for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
                    GoodsInfo goodsInfo = GoodsMgr.getGoodsById(entry.getKey());
                    if (goodsInfo == null) {
                        continue;
                    }
                    if (goodsInfo.getType() == eGoodsType.WaSheSkillBox.getValue()) {
                        //技能宝箱
                        BaseUseGoods useGoodsEntity = BaseUseGoods.getUseGoodsEntity(player, goodsInfo, 0, "");
                        if(null == useGoodsEntity){
                            continue;
                        }
                        int count = entry.getValue().intValue();
                        UseGoodsResult result = useGoodsEntity.useGoods(count);
                        if(result.getRet() == 0){
                            reward.addProperty(PropertyHelper.parseStringToProperty(result.getReward()));
                        }else {
                            log.error("小玉购买 技能宝箱 {}={}, 打开失败{}，{}", entry.getValue().toString(), count, 0, player.getUserId());
                        }
                    } else {
                        WasheConfig washeConfig = WasheMgr.getWasheConfig();
                        if (washeConfig != null && washeConfig.getCARD_BOX_ID().contains(entry.getKey())) {
                            //优伶卡
                            BaseUseGoods useGoodsEntity = BaseUseGoods.getUseGoodsEntity(player, goodsInfo, 0, "");
                            if(null == useGoodsEntity){
                                continue;
                            }
                            int count = entry.getValue().intValue();
                            UseGoodsResult result = useGoodsEntity.useGoods(count);
                            if(result.getRet() == 0){
                                reward.addProperty(PropertyHelper.parseStringToProperty(result.getReward()));
                            }else {
                                log.error("小玉购买 优伶卡 {}={}, 打开失败{}，{}", entry.getValue().toString(), count, washeConfig.getActivityInfo().getActivityId(), player.getUserId());
                            }
                        } else {
                            //普通道具
                            reward.addProperty(entry.getKey(), entry.getValue());
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("", e);
        }
    }

    public int zsSet(AssistantProto.AssistantZsSetReqMsg reqMsg) {
        //按照vip等级获取项目开启倍数
        int limitMultiple = player.getModule(ZsModule.class).getLimitMultiple();
        int maxNum = ZsConfigMgr.getZsProjectInfoMap().size();

        List<Integer> typeList = new ArrayList<>();
        for (Integer zsType : reqMsg.getTypeListList()) {
            ZsProjectInfo zsProjectInfo = ZsConfigMgr.getByType(zsType);
            if (zsProjectInfo == null) {
                return GameErrorCode.E_ZS_PROJECT_CONFIG_NOT_FOUND;
            }

            if (typeList.contains(zsType)) {
                return GameErrorCode.E_ZS_PROJECT_ASSISTANT_REPEAT;
            }

            typeList.add(zsType);

            if (typeList.size() > maxNum) {
                return GameErrorCode.E_ERROR_PARAMETER;
            }
        }

        synchronized (this) {
            if (userAssistantZsSetData == null) {
                userAssistantZsSetData = new UserAssistantZsSetData(player.getUserId());
            }
            userAssistantZsSetData.setOpen(reqMsg.getIsOpen());
            userAssistantZsSetData.setTypeList(typeList);
        }

        syncZsSet();
        return 0;
    }

    public void syncZsSet() {
        if (userAssistantZsSetData == null) {
            return;
        }
        AssistantProto.SyncAssistantZsSetMsg.Builder syncMsg = AssistantProto.SyncAssistantZsSetMsg.newBuilder();
        synchronized (this) {
            syncMsg.setIsOpen(userAssistantZsSetData.isOpen());
            syncMsg.addAllTypeList(userAssistantZsSetData.getTypeList());
        }

        player.sendPacket(ClientProtocol.U_ASSISTANT_ZS_SET_SYNC, syncMsg);
    }

    public UserAssistantZsSetData getUserAssistantZsSetData() {
        return userAssistantZsSetData;
    }

    public List<Integer> getZsTypeList() {
        if (userAssistantZsSetData == null || !userAssistantZsSetData.isOpen()) {
            return new ArrayList<>();
        }
        return userAssistantZsSetData.getTypeList();
    }

    class MallSort implements Comparator<AssistantShopSet> {
        @Override
        public int compare(AssistantShopSet o1, AssistantShopSet o2) {
            MallInfo mallInfo1 = MallMgr.getMallInfoByMallId(o1.getMallId());
            MallInfo mallInfo2 = MallMgr.getMallInfoByMallId(o2.getMallId());
            //先比较限购类型 每月限购3 每日限购1 月的排在后面
            if (mallInfo1.getLimitBuyType() < mallInfo2.getLimitBuyType()) {
                return -1;
            } else if (mallInfo1.getLimitBuyType() > mallInfo2.getLimitBuyType()) {
                return 1;
            } else {
                //商会商店 还有食材分页 排在普通道具后面 月限购之前
                if (mallInfo1.getShopType() == eShopType.UnionMall.getValue() && (isDishesMaterialMall(mallInfo1) || isDishesMaterialMall(mallInfo2))) {
                    if (!isDishesMaterialMall(mallInfo1) && isDishesMaterialMall(mallInfo2)) {
                        return -1;
                    } else if (isDishesMaterialMall(mallInfo1) && !isDishesMaterialMall(mallInfo2)) {
                        return 1;
                    }
                }
            }
            //根据sort字段进行排序
            if (mallInfo1.getSort() < mallInfo2.getSort()) {
                return -1;
            } else if (mallInfo1.getSort() > mallInfo2.getSort()) {
                return 1;
            }
            return 0;
        }
    }

    public static boolean isDishesMaterialMall(MallInfo mallInfo) {
        Map<Integer, BigInteger> goods = mallInfo.getRewardProp().getGoods();
        for (Integer goodsId : goods.keySet()) {
            if (isDishesMaterialId(goodsId)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否是食材id
     * @param goodsId
     * @return
     */
    public static boolean isDishesMaterialId(int goodsId) {
        return goodsId >= 117000 && goodsId <= 117999;
    }

    /**
     * 转换为 shopType-list Map
     * @return
     */
    private Map<Integer, List<AssistantShopSet>> transferShopTypeMap() {
        Map<Integer, List<AssistantShopSet>> map = new ConcurrentHashMap<>();
        for (AssistantShopSet shopSet : assistantShopSetMap.values()) {
            MallInfo mallInfo = MallMgr.getMallInfoByMallId(shopSet.getMallId());
            if (mallInfo != null) {
                if (!map.containsKey(mallInfo.getShopType())) {
                    map.put(mallInfo.getShopType(), new ArrayList<>());
                }
                map.get(mallInfo.getShopType()).add(shopSet);
            }
        }
        return map;
    }

    /**
     * 小助理商店排序列表
     * @return
     */
    private List<Integer> assistantShopTypeOrderList() {
        List<Integer> assistantShopTypeList = new ArrayList<>();
        //乔迁风云商城
        assistantShopTypeList.add(eShopType.EstateWarMall.getValue());
        //商战积分商城
        assistantShopTypeList.add(eShopType.TradeWarIntegralMall.getValue());
        //商会商城
        assistantShopTypeList.add(eShopType.UnionMall.getValue());
        //酒楼商店
        assistantShopTypeList.add(eShopType.BanquetMall.getValue());
        //狩猎商店
        assistantShopTypeList.add(eShopType.HuntMall.getValue());
        //客栈商店
        assistantShopTypeList.add(eShopType.InnMall.getValue());
        //皇城兑换商店
        assistantShopTypeList.add(eShopType.PalaceExchangeMall.getValue());
        //酒坊兑换商店
        assistantShopTypeList.add(eShopType.WineryExchangeMall.getValue());

        //蛐蛐兑换商店
        assistantShopTypeList.add(eShopType.CricketExchangeMall.getValue());

        //瓦舍兑换商店
        //解锁才加
        if(SystemOpenMgr.systemOpen(player, eSystemId.WasheActivity.getValue())) {
            assistantShopTypeList.add(eShopType.WaSheExchangeActivityMall.getValue());
        }

        return assistantShopTypeList;
    }

    private AssistantProto.AssistantShopSetTemp.Builder parseAssistantGoodsSetTemp(AssistantShopSet assistantShopSet) {
        AssistantProto.AssistantShopSetTemp.Builder temp = AssistantProto.AssistantShopSetTemp.newBuilder();
        temp.setMallId(assistantShopSet.getMallId());
        temp.setCount(assistantShopSet.getCount());
        return temp;
    }

    private AssistantProto.SyncAssistantShopSetMsg.Builder parseSyncMsg(List<AssistantShopSet> assistantShopSetList) {
        AssistantProto.SyncAssistantShopSetMsg.Builder syncMsg = AssistantProto.SyncAssistantShopSetMsg.newBuilder();
        for (AssistantShopSet assistantShopSet : assistantShopSetList) {
            syncMsg.addShopSet(parseAssistantGoodsSetTemp(assistantShopSet));
        }
        return syncMsg;
    }

    public void clearUnionMallSet() {
        syncAssistantShopSet();
    }


    @Override
    public boolean saveData() {
        for (AssistantShopSet data : assistantShopSetMap.values()) {
            if (data.isInsertOption()) {
                UserAssistantShopBusiness.addData(data);
            } else if (data.isUpdateOption()) {
                UserAssistantShopBusiness.updateData(data);
            }
        }

        if (userAssistantZsSetData != null) {
            if (userAssistantZsSetData.isInsertOption()) {
                new UserAssistantZsSetDaoImpl().add(userAssistantZsSetData);
            } else if (userAssistantZsSetData.isUpdateOption()) {
                new UserAssistantZsSetDaoImpl().update(userAssistantZsSetData);
            }
        }
        if (settingUserData != null) {
            if (settingUserData.isInsertOption()) {
                new AssistantSettingUserDataDaoImpl().add(settingUserData);
            } else if (settingUserData.isUpdateOption()) {
                new AssistantSettingUserDataDaoImpl().update(settingUserData);
            }
        }
        return true;
    }

    public AssistantProto.AssistantActionRespMsg.Builder getActionBuilder() {
        return actionBuilder;
    }

    public boolean clearActionBuilder(){
        if(this.actionBuilder == null){
            return false;
        }
        this.actionBuilder = null;
        return true;
    }

    public boolean setActionBuilder(AssistantProto.AssistantActionRespMsg.Builder actionBuilder) {
        if(this.actionBuilder != null){
            return false;
        }
        this.actionBuilder = actionBuilder;
        return true;
    }
}
